import Plugin from './plugin'
import {CN,EN,Cst} from '../config'

export default class AbstractPlugin extends Plugin{
	constructor(...a){
		super(...a)
	}
	__init(cfg){
		// this.createClassName({
		// 	panel:"panel",
		// 	btn:"action-btn"
		// })
		// this.config
		// let cfg = this.config
		/** {
		// 	layer:true,
		// 	layerClass:[CN.tlayer,CN.hide].join(" ")
		**/ 
		cfg.cssText&&this.injectStyle(cfg.cssText,this.cn)
		cfg.layer&&this.createLayer([CN.hide,cfg.layerClass||''].join(" "))
		this.__render(cfg.panelTag)
		this.__bind()
	}
	__render(panelTag){
		let cn = this.cn
		let panel =
		this.panel = document.createDOM(panelTag||"div",{
			class:[cn.panel,CN.abs].join(" ")
		})
		this.$layer.appendChild(panel)
	}
	__bind(){
		this.$layer.addEventListener("click",()=>{
			this.emit("onLayerClick")
			this.focusOut()
		})
		this.panel.addEventListener("click",(e)=>{
			e.stopPropagation()
			if(!e.target.dataset.action)return
			this.emit(EN.click,e)
		})

		this.on(Cst.focusOut,this.onFocusOut.bind(this))
		this.on(Cst.focusIn,(...a)=>{
			this.$layer&&this.$layer.classList.remove(CN.hide)
			this.onFocusIn&&this.onFocusIn(...a)
		})

		this.on(EN.click,(...a)=>{
			this.onClick&&this.onClick(...a)
		})

	}
	onFocusOut(){
		this.$layer.classList.add(CN.hide)
	}
}